package controllers

import (
	"fmt"
	"gitee.com/tsingeye/FreeEhome/models"
	"gitee.com/tsingeye/FreeEhome/tools/logs"
	"github.com/astaxie/beego"
	"time"
)

type HookController struct {
	beego.Controller
}

/*on_stream_none_reader body:
{
   "app" : "live",
   "mediaServerId" : "Y148behloruyBEIL",
   "schema" : "fmp4",
   "stream" : "1824858144", //实时直播设备返回的session
   "vhost" : "__defaultVhost__"
}
*/
//关闭流通知
func (this *HookController) OnStreamNoneReader() {
	logs.BeeLogger.Emergency("HookController.OnStreamNoneReader() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnStreamNoneReader() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)

	this.Data["json"] = models.OnStreamNoneReader(this.Ctx.Input.RequestBody)
	this.ServeJSON()
}

/*
on_publish body:
{
   "app" : "live",
   "id" : "319651707728",
   "ip" : "192.168.1.192",
   "mediaServerId" : "Y148behloruyBEIL",
   "params" : "",
   "port" : 54419,
   "schema" : "rtmp",
   "stream" : "1400772866",
   "vhost" : "__defaultVhost__"
}
*/
func (this *HookController) OnPublish() {
	logs.BeeLogger.Emergency("HookController.OnPublish() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnPublish() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)

	this.Data["json"] = models.OnPublish(this.Ctx.Input.RequestBody)
	this.ServeJSON()
}

/*
on_play:
{
   "mediaServerId" : "your_server_id",
   "app" : "live",
   "id" : "140574554588960",
   "ip" : "10.0.17.132",
   "params" : "",
   "port" : 65217,
   "schema" : "rtmp",
   "stream" : "obs",
   "vhost" : "__defaultVhost__"
}
*/
func (this *HookController) OnPlay(){
	logs.BeeLogger.Emergency("HookController.OnPlay() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnPlay() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)

	this.Data["json"] = models.OnPlay(this.Ctx.Input.RequestBody)
	this.ServeJSON()
}

/*
on_record_mp4 body:
{
   "app" : "ehrecord",
   "file_name" : "19-00-32.mp4",
   "file_path" : "/service/sms/www/record/ehrecord/1661453800/2021-05-09/19-00-32.mp4",
   "file_size" : 892208997,
   "folder" : "/service/sms/www/record/ehrecord/1661453800/",
   "mediaServerId" : "your_server_id",
   "start_time" : 1620558032,
   "stream" : "1661453800",
   "time_len" : 412,
   "url" : "record/ehrecord/1661453800/2021-05-09/19-00-32.mp4",
   "vhost" : "__defaultVhost__"
}
//合成路径格式：http://139.129.116.64:8090/record/ehrecord/1847434072/2021-05-09/19-11-53.mp4
http://streamIP[1]:hlsPort[1]/record/ehrecord/1661453800/2021-05-09/19-00-32.mp4
*/
func (this *HookController) OnRecordMP4() {
	logs.BeeLogger.Emergency("HookController.OnRecordMP4() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnRecordMP4() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)

	this.Data["json"] = models.OnRecordMP4(this.Ctx.Input.RequestBody)
	this.ServeJSON()
}

func (this *HookController) OnHttpAccess() {
	logs.BeeLogger.Emergency("HookController.OnHttpAccess() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnHttpAccess() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)
}

/*
{
   "mediaServerId" : "your_server_id",
   "app" : "live",
   "id" : "140183261486112",
   "ip" : "10.0.17.132",
   "params" : "",
   "port" : 49614,
   "schema" : "rtsp",
   "stream" : "obs",
   "vhost" : "__defaultVhost__"
}
*/
func (this *HookController) OnStreamNotFound() {
	logs.BeeLogger.Emergency("HookController.OnStreamNotFound() ---> data:%s", this.Ctx.Input.RequestBody)
	fmt.Printf("%s HookController.OnStreamNotFound() ---> data:%s\n", time.Now().Format("2006-01-02 15:04:05"), this.Ctx.Input.RequestBody)

   this.Data["json"] = models.OnStreamNotFound(this.Ctx.Input.RequestBody)
   this.ServeJSON()
}
